package sk.ogame.brain;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sk.ogame.brain.thing.CarryThing;
import sk.ogame.brain.thing.PlanetThing;
import sk.ogame.brain.thing.Thing;
import sk.ogame.realm.exception.PlanetNoInfoException;
import sk.ogame.realm.exception.ResourceNotEnoughException;

public class ExceptionMind {

    public static final Logger logger = LoggerFactory.getLogger(ExceptionMind.class);
    public static final String packageName = "sk.ogame.brain.thing";


    public static Thing deal(Exception e) {
        /*
        if (!(e instanceof BaseException)){
            e.printStackTrace();
            throw new BaseException("Not support exception !", 0);
        }
         */
        if (e instanceof PlanetNoInfoException) {
            PlanetNoInfoException noInfoException = (PlanetNoInfoException) e;
            switch (noInfoException.getErrCode()) {
                case PlanetNoInfoException.CODE_PLANET:
                    return new PlanetThing(noInfoException.getPlanetId(), PlanetThing.Operate.getInfo);
                case PlanetNoInfoException.CODE_OVERVIEW:
                    return new PlanetThing(0, PlanetThing.Operate.getOverview);
            }
        } else if (e instanceof ResourceNotEnoughException) {
            ResourceNotEnoughException rne = (ResourceNotEnoughException) e;
            CarryThing carryThing = new CarryThing();
            carryThing.setTargetId(rne.getPlanetId());
            carryThing.setMetal(rne.getNeedMetal());
            carryThing.setCrystal(rne.getNeedCrystal());
            carryThing.setDeuterium(rne.getNeedDeuterium());
            return carryThing;
        }

        e.printStackTrace();
        System.exit(1);
        logger.error("can not deal exception : {}", e.getMessage());
        return null;
    }

    /*
    static{
        Set<Class<?>> clsSet = new HashSet<>();
        try {
            Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(packageName.replace('.','/'));
            while (resources.hasMoreElements()){
                URL url = resources.nextElement();
                if ("file".equals(url.getProtocol())){
                    String filePath = URLDecoder.decode(url.getFile(),"UTF-8");
                    findAndAddClassesInPackageByFile(packageName, filePath,false,clsSet);
                }
            }

            for (Class<?> cls : clsSet){
                if (Modifier.isAbstract(cls.getModifiers())){
                    continue;
                }
                int modifiers = cls.getModifiers();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void findAndAddClassesInPackageByFile(String packageName, String packagePath, final boolean recursive,
                                                        Set<Class<?>> classes) {
        // 获取此包的目录 建立一个File
        File dir = new File(packagePath);
        // 如果不存在或者 也不是目录就直接返回
        if (!dir.exists() || !dir.isDirectory()) {
            // log.warn("用户定义包名 " + packageName + " 下没有任何文件");
            return;
        }
        // 如果存在 就获取包下的所有文件 包括目录
        File[] dirfiles = dir.listFiles(new FileFilter() {
            // 自定义过滤规则 如果可以循环(包含子目录) 或则是以.class结尾的文件(编译好的java类文件)
            public boolean accept(File file) {
                return (recursive && file.isDirectory()) || (file.getName().endsWith(".class"));
            }
        });
        // 循环所有文件
        for (File file : dirfiles) {
            if (file.getName().contains("$")){
                continue;
            }
            // 如果是目录 则继续扫描
            if (file.isDirectory()) {
                findAndAddClassesInPackageByFile(packageName + "." + file.getName(), file.getAbsolutePath(), recursive,
                        classes);
            } else {
                // 如果是java类文件 去掉后面的.class 只留下类名
                String className = file.getName().substring(0, file.getName().length() - 6);
                try {
                    // 添加到集合中去
                    // classes.add(Class.forName(packageName + '.' +
                    // className));
                    // 经过回复同学的提醒&#xff0c;这里用forName有一些不好&#xff0c;会触发static方法&#xff0c;没有使用classLoader的load干净
                    classes.add(
                            Thread.currentThread().getContextClassLoader().loadClass(packageName + '.' + className));
                } catch (ClassNotFoundException e) {
                    // log.error("添加用户自定义视图类错误 找不到此类的.class文件");
                    e.printStackTrace();
                }
            }
        }
    }
     */

}
